package com.hougong.im.exception;

import com.hougong.im.utils.R;
import io.jsonwebtoken.JwtException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class RRExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(IMException.class)
    public R handleRRException(IMException e){
        R r = new R();
        r.put("code", e.getCode());
        r.put("msg", e.getMessage());
        logger.error(e.getMessage(),e);
        return r;
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public R handleDuplicateKeyException(DuplicateKeyException e){
        logger.error(e.getMessage(), e);
        return R.error("数据库中已存在该记录");
    }

    @ExceptionHandler(JwtException.class)
    public R handleAuthorizationException(JwtException e){
        logger.error(e.getMessage(), e);
        return R.error(" token无效,请重新登录 ");
    }

    @ExceptionHandler(Exception.class)
    public R handleException(Exception e){
        logger.error(e.getMessage(), e);
        return R.error(e.getMessage());
    }
}
